"""
# 1.爬取136book网VIP小说：花千骨
# 2.URL：http://www.136book.com/huaqiangu/
"""
from bs4 import BeautifulSoup
import requests
import re


url = 'http://hezumeinv.136book.com/'
res = requests.get(url)
res.encoding = 'utf-8'
soup = BeautifulSoup(res.text, 'html.parser')
book_detail_text = soup.select('.box1')[1]
book_detail_list = book_detail_text.select('a')
all_href = []
write_text = open('D:/合租美女.txt', 'w')
print('写入目录')
for book_list in book_detail_list:
    a_href = book_list.get('href')
    all_href.append(a_href)
    a_text = book_list.text
    write_text.write(a_text+'\n')
print('目录完成')
write_text.close()


print('准备写入正文')
write_context = open('D:/合租美女.txt', 'a')
for text_url in all_href:
    text_res = requests.get(text_url)
    text_res.encoding = 'utf-8'
    text_soup = BeautifulSoup(text_res.text, 'html.parser')

    title_list = text_soup.select('h1')[0].text
    print('准备写入:{}……'.format(title_list))
    write_context.write(title_list+'\n')
    context_list = text_soup.find_all('p')
    page_one = ''.join(str(context_list))
    page_one = re.sub('\[', '', ''.join(page_one))
    page_one = re.sub('\]', '', ''.join(page_one))
    page_one = re.sub(',', '', ''.join(page_one))
    page_one = re.sub('<p>', '', ''.join(page_one))
    page_one = re.sub('</p>', '', ''.join(page_one))

    write_context.write(page_one+'\n')
    print('{}写入完成。'.format(title_list))

write_context.close()
print('全文下载完成。')











